<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body style="background-color: #ccc;">
<div id="app">
    <div>商品名称：<input v-model="name"/></div>
    <button v-on:click="cut">减一个</button>
    购买数量{{count}}
    <button v-on:click="add">加一个</button>
    <button v-on:click="addCart">加入购物车</button>
    <div v-for="(item, index) in list" :key="index">
        {{item.name}}  x{{item.count}}
    </div>
</div>
</body>
<script src="../../js/vue.js"></script>
<script type="text/javascript">
    var vm = new Vue({
        el: '#app',
        data: {
            name: '',
            count:0,
            isMax: false,
            list: []
        },
        methods: {
            cut() {
                this.count = this.count - 1
                this.isMax = false
            },
            add() {
                this.count = this.count + 1
            },
            addCart() {
                this.list.push({
                    name: this.name,
                    count: this.count
                })
            }
        },
        watch: {
            count: function(newVal, oldVal) {
                if(newVal > 10) {
                    this.isMax = true
                }
                if(newVal < 0) {
                    this.count = 0
                }
            },
            name: function() {
                this.count = 0
                this.isMax = false
            },
            isMax: function(newVal) {
                if (newVal) {
                    console.log('注意：您购买的数量较大，请确认是否操作有误')
                }
            },
            list() {
                console.log('购物车数据发生改变')
            }
        }
    })
</script>
</html>
